clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
* data from FE_MO_Feb.do
use permco permno statpers medest* street_share_adj1 street_share_adj3 using $Work/FE_MO.dta, clear
gen datem=mofd(statpers)
format datem %tm

tsset permno datem
gen cpMo1=ln(F1.medest1)-ln(medest2)
gen cpQr1=ln(F3.medest1)-ln(medest4)

gstats winsor cp*1, cuts(1 99) by(datem) replace  
keep permco permno datem cp*
* ***********************************************************************************************
* data from read_ibes.do
merge 1:1 permno permno datem using  $Work/XSibes.dta, keepusing(permno permco datem statpers datem LTG) 
drop _merge

tsset permno datem
replace LTG=LTG/100
gen L1LTG=L12.LTG

gen dMFLTG=F1.LTG-LTG
gen dQFLTG=F3.LTG-LTG

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
}
merge 1:1 permco permno datem using $Work/EBR_new_monthly
drop if _merge==1
drop _merge
d cp* FE* 

* data from Firms01_June.do
merge 1:1 permco permno datem using $Work/Firms01_June, keepusing(permco permno datem INV BOOK MOME SIZE SI10 MOME STG2 dp)
drop if _merge==2
drop _merge
drop SIZE
rename SI10 SIZE
gstats winsor LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2, cuts(1 99) by(datem) replace
tempfile tmpstuff
save "`tmpstuff'", replace
* ************************************************
foreach group in BOOK {
use if `group'+qruf1<. using "`tmpstuff'", clear
generate portf=1+int((`group'-1)/2)
order *, sequential    
gcollapse (mean) LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 ret_mo1-ret_mo60, by(datem portf)

gen  Nret1Yr=1 
gen  Nret2Yr=1 
gen  Nret3Yr=1 
gen  Nret4Yr=1 
gen  Nret5Yr=1
forvalues x=1(1)60 {
	replace ret_mo`x'=ret_mo`x'-1
}	 
forvalues x=1(1)12 {
	replace Nret1Yr=Nret1Yr*(1+ret_mo`x')
}
forvalues x=13(1)24 {
	replace  Nret2Yr=Nret2Yr*(1+ret_mo`x')
}
forvalues x=25(1)36 {
	replace  Nret3Yr=Nret3Yr*(1+ret_mo`x')
}
forvalues x=37(1)48 {
	replace  Nret4Yr=Nret4Yr*(1+ret_mo`x')
}
forvalues x=49(1)60 {
	replace  Nret5Yr=Nret5Yr*(1+ret_mo`x')
}
*********************
scalar alfa= 0.97749304
gen cumNRetMo1=ln(1+ret_mo1)
gen cumNRetQr1=ln(1+ret_mo1)+ln(1+ret_mo2)+ln(1+ret_mo3)
gen cumNRetYr1=ln(Nret1Yr)
gen cumNRetYr3=ln(Nret1Yr)+alfa^1*ln(Nret2Yr)+alfa^2*ln(Nret3Yr)
gen cumNRetYr5=ln(Nret1Yr)+alfa^1*ln(Nret2Yr)+alfa^2*ln(Nret3Yr)+alfa^3*ln(Nret4Yr)+alfa^4*ln(Nret5Yr)
*********************
tempfile tmp
save "`tmp'", replace
*********************
keep if portf==1 | portf==5 
foreach var of varlist  cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
replace `var'=-`var' if portf==5 & index("`PORTF'","INV")+index("`PORTF'","BETA")+index("`PORTF'","SIZE")==1
replace `var'=-`var' if portf==1 & index("`PORTF'","INV")+index("`PORTF'","BETA")+index("`PORTF'","SIZE")==0
}
foreach var of varlist cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
	bys datem: egen n`var'=count(`var')
}
foreach var of varlist cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
	replace `var'=. if n`var'<2
	drop n`var'
}
gcollapse (sum)        cumNRet* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2,  by(datem)
foreach var of varlist cum* LTG L1LTG d1LLTG dp FE* cp* d*FLTG STG2 {
	replace `var'=. if `var'==0
}
generate portf=6

append using `tmp'
sort portf datem
*********************
tsset portf datem
foreach var of varlist FE1-FE5 cp1-cp5 d*FLTG {
	quietly: bys portf: egen sd=sd(`var')
	quietly: replace  `var'=`var'/sd	
	drop sd
}
eststo clear
foreach p of numlist 6 {
gen FE=FE1
gen cp=cp1
gen dFLTG=d1FLTG
eststo new_`p'_3: quietly: ivreg2 cumNRetYr1 FE cp dFLTG 	if portf==`p', bw(13)
drop FE cp dFLTG

gen FE=FE3
gen cp=cp3
gen dFLTG=d3FLTG
eststo new_`p'_4: quietly: ivreg2 cumNRetYr3 FE cp dFLTG 	if portf==`p', bw(37)
drop FE cp dFLTG

gen FE=FE5
gen cp=cp5
gen dFLTG=d5FLTG
eststo new_`p'_5: quietly: ivreg2 cumNRetYr5 FE cp dFLTG 	if portf==`p', bw(61)
drop FE cp dFLTG
}
esttab, order(FE cp dFLTG)
local file "Table04_June"
if "`group'"=="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) replace  title(`group')
}
if "`group'"!="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) append  title(`group')
}
**********************************************
}
